import paddle
from paddle.io import Dataset
import os
import numpy as np
from paddle.vision.transforms import Normalize
import cv2



class Mydata(Dataset):
    def __init__(self,train_data_base_dir,label_data_base_dir,transform=None):
        super().__init__()
        self.train_data_base_dir=train_data_base_dir
        self.label_data_base_dir=label_data_base_dir
        self.image_data_list=[]
        files=os.listdir(label_data_base_dir)
        for file in files:
            file=os.path.join(self.label_data_base_dir,file)
            label_txt=file
            print(label_txt)
            with open(label_txt,'r',encoding='UTF-8') as label_content:
                label_content=label_content.readlines()
                print(label_content)
                label_content = [x.split(',') for x in label_content]
                print(label_content)
                image_path=label_content[0][0]
                label=label_content[0][1]
                image_dir=os.path.join(self.train_data_base_dir,image_path)
                self.image_data_list.append([image_dir,label])
                np.random.shuffle(self.image_data_list)
        i=0
        for data in self.image_data_list:

            print(self.image_data_list[i][1])
            i=i+1



        self.transform=transform
    def __getitem__(self,index):
        img_path,label=self.image_data_list[index]
        img=cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
        img=img.astype('float32')
        if self.transform is not None:
            img=self.transform(img)
        label=int(label)
        channels=img.shape[1]
        # print("图片的形状是是:",img.shape)
        return img,label
    def __len__(self):
        return len(self.image_data_list)





